<template>

  <div class="m-content">
    header
    <h3>欢迎来到MarkerHub博客</h3>

    <div class="block">
      <el-avatar :size="50" :src="user.avatar"></el-avatar>
      <div>{{user.username}}</div>
    </div>

    <div class="maction">
      <span><el-link href="/blogs">主页</el-link></span>
      <el-divider direction="vertical"></el-divider>
      <span><el-link type="success" href="/blog/add">发表博客</el-link></span>

      <el-divider direction="vertical"></el-divider>
      <span v-show="!hasLogin"><el-link type="primary" href="/login">登录</el-link></span>

      <span v-show="hasLogin"><el-link type="danger" @click="logout">退出</el-link></span>
    </div>

  </div>

</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      user: {
        username: '请先登录', // 默认属性
        avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
      },
      hasLogin: false
    }
  },
  methods: {
    logout() {   //由于该项目比较简单，jwt是无状态的并未存入Redis中，所以这里可简单地清空localstorage即可
      const _this = this
      _this.$axios.get("/logout",{   //由于后端设置了需要用户认证之后才能走 "/logout请求"，所以这里要配置一下
        headers: {
          "Authorization": localStorage.getItem("token")
        }
      }).then(res => {  //返回
        _this.$store.commit("REMOVE_INFO")
        _this.$router.push("/login")
      })
    }
  },
  created() {   //页面一创建时就回显数据
    if(this.$store.getters.getUser.username) {

      this.user.username = this.$store.getters.getUser.username
      this.user.avatar = this.$store.getters.getUser.avatar

      this.hasLogin = true

    }
  }
}
</script>

<style scoped>

  .m-content {
    max-width: 960px;
    margin: 0 auto;
    text-align: center;
  }

  .maction {
    margin: 10px 0;
  }

</style>